<template>
  <div class="app-container">
    <!-- 搜索栏 -->
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
      <el-form-item label="账本名字" prop="accountBook">
        <el-input
          v-model="queryParams.accountBook"
          placeholder="请输入分类名字"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <div class="block">
          <span class="demonstration">月</span>
          <el-date-picker
            v-model="month"
            type="month"
            placeholder="选择月">
          </el-date-picker>
        </div>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        >
        </el-date-picker>
          </el-form-item>
      <!-- <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="菜单状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 按钮栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:menu:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click=""
          v-hasPermi="['system:menu:add']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table
      v-loading="loading"
      :data="billList"
      row-key="id"
    >
      <el-table-column prop="id" label="ID" :show-overflow-tooltip="true" width="160"></el-table-column>
      <el-table-column prop="accountBookId" label="账本id" :show-overflow-tooltip="true" width="160"></el-table-column>
      <el-table-column prop="billCategoryId" label="账本分类id" :show-overflow-tooltip="true" width="160"></el-table-column>
      <el-table-column prop="accountBookName" label="账本名称" :show-overflow-tooltip="true" width="160"></el-table-column>
      <el-table-column prop="billCategoryName" label="类别名称" :show-overflow-tooltip="true" width="160"></el-table-column>
      <!-- <el-table-column prop="name" label="名称" width="160"></el-table-column> -->
      <el-table-column prop="money" label="花费" width="160"></el-table-column>
      <el-table-column prop="createTime" label="创建时间" :show-overflow-tooltip="true"></el-table-column>
      <el-table-column prop="createUserAccount" label="创建人账户" :show-overflow-tooltip="true"></el-table-column>
      <el-table-column prop="remark" label="备注" align="center" width="200"></el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:menu:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:menu:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改菜单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="680px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="账本名称" prop="accountBookId">
          <el-select v-model="form.accountBookId">
            <el-option
            v-for="item in accountBookList"
            :key="item.id"
            :label="item.name"
            :value="item.id">

            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select v-model="form.type" placeholder="类型" clearable size="small">
            <el-option
              v-for="dict in typeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
      </el-form-item>
      <el-row>
        <el-col>
          <el-form-item label="一级分类" prop="firstCategory">
            <el-select 
              v-model="form.firstCategory"
              @change="getSecondCategories"
              @blur="log">
              <el-option 
                v-for="item in this.firstCategory"
                :key="item.id"
                :label="item.name"
                :value="item.id">

              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="二级分类" prop="secondCate">
            <el-select 
              v-model="form.secondCategory">
              <el-option 
                v-for="item in this.secondCategory"
                :key="item.id"
                :label="item.name"
                :value="item.id">

              </el-option>
            </el-select>
      </el-form-item>
        </el-col>    
      
      </el-row>
        <el-row>
          <el-col :span="18">
            <el-form-item label="金额" prop="money">
              <el-input v-model="form.money" placeholder="请输入金额" maxlength="100" />
              </el-form-item>
            </el-col>
        </el-row>
        <el-row>
          <el-col :span="18">
            <el-form-item label="支出日期" 
              prop="createTime"
              v-show="form.type===1">
              <el-date-picker 
                v-model="form.createTime" 
                placeholder="请输入金额" 
                value-format="yyyy-MM-dd">
              </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="18">
            <el-form-item label="收入日期" 
              prop="createTime"
              v-show="form.type===2">
              <el-date-picker v-model="form.createTime" placeholder="请输入金额" value-format="yyyy-MM-dd">
              </el-date-picker>
              </el-form-item>
            </el-col>
        </el-row>
        <el-row>
          <el-col :span="18" >
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" placeholder="请输入备注" maxlength="100" />
              </el-form-item>
            </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    
  </div>
</template>

<script>
import { listBill, addBill } from "@/api/bill";
import { listAccountBook } from "@/api/bill";
import { searchFirstCategory, searchSecondCategory} from "@/api/bill"
export default {
  name: "startBill",
  
  data() {
    return {
      // 搜索月份
      month:'',
      // 遮罩层
      loading: true,
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 表单参数
      form: {
        accountBook:'',
        type:1
      },
      // 弹出层标题
      title: "",
      // 显示搜索条件
      showSearch: true,
      accountBookList:[],
      // 一级分类
      firstCategory:[],
      // 二级分类
      secondCategory:[],
      billList: [],
      total:0,
      queryParams:{
        pageNum: 1,
        pageSize: 10,
        accountBook: undefined,
        
      },
      // 类别数据字典
      typeOptions:[
        {
          "remark": "支出类别",
          "dictLabel": "支出",
          "dictValue": 1,
        },
        {
          "remark": "收入类别",
          "dictLabel": "收入",
          "dictValue": 2,
        }
      ],
      // 表单校验
      rules: {
        firstCategory:[
          { required: true, message: "一级分类不能为空", trigger: "change"}
        ],
        // secondCate:[
        //   { required: true, message: "二级分类不能为空", trigger: "blur"}
        // ],
        money: [
          { required: true, message: "花费不能为空", trigger: "blur" }
        ],
       
      }
    }
  },
  created() {
    this.getList();
  },
  methods: {
    log() {
      console.log('blur')
    },
    // 重置form表单字段
    reset() {
      this.form = {
        accountBook:'',
        type: 1,
        remark:''
      }
      
      // 对refname=form的整个表单进行重置，将所有字段值重置为初始值并移除校验结果
      this.resetForm("form");
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 提交按钮 */
    submitForm: function() {
      this.form.billCategoryId = this.form.secondCategory || this.form.firstCategory
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateBill(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBill(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.getAccountBookList()
      this.getFirstCategories()
      if (row != null && row.id) {
        // this.form.parentMenuId = row.id;
      } else {
        // this.form.parentMenuId = 0;
      }
      this.open = true;
      this.title = "记账";
    },
    getAccountBookList() {
      listAccountBook().then(response => {
        this.accountBookList = response.rows
        console.log(response.rows);
        this.form.accountBookId = this.accountBookList[0].id
      })
    },
    getFirstCategories() {
      searchFirstCategory({id:1}).then(response => {
        this.firstCategory = response.data
      })
    },
    getSecondCategories() {
      const data = {}
      data.id = this.form.firstCategory
      searchSecondCategory(data).then(response => {
        this.secondCategory = response.data
      })
    },
    getList() {
      listBill(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
       this.billList = response.rows
       this.total = response.total
       this.loading = false;
      });
    }
  }
}
</script>

<style>

</style>